
import { reactive } from "vue";
import {  getBooksByCondationApi } from "@/api/crud";

export function useBooks(bookData){
    const searchData = reactive({
        "id": "",
        "name": "",
        "author": "",
        "minprice": "",
        "maxprice": ""
    })
    
    const searchFn = () => {
    
        const temp = {};
        for (const key in searchData) {
            if (searchData[key] !== "") {
                temp[key] = searchData[key];
            }
        }
    
        if (temp["minprice"]) {
            delete temp["minprice"];
            temp["price_gte"] = searchData.minprice;
        }
    
        if (temp["maxprice"]) {
            delete temp["maxprice"];
            temp["price_lte"] = searchData.maxprice;
        }
    
    
        getBooksByCondationApi(temp)
            .then(res => {
                if (res.data) {
                    bookData.length = 0;
                    bookData.push(...res.data);
                }
            })
    }

    return {
        searchData,searchFn
    }
}